package com.wzr.comparetool;

import java.util.ArrayList;
import java.util.TreeMap;

/**
 * 根据传入的扫描结果, 解析出表结果和视图结果
 */
public class DBReport {

	public static final int DBTITLE_LINES = 4;

	public String getDbTitle() {
			return dbTitle;
//			return (dbTitle.charAt(dbTitle.length() - 1) == '\n') ? dbTitle.substring(0,dbTitle.length() - 1) : dbTitle;
	}

	public String getTableTitle() {
		return tableTitle;
	}

	public TreeMap<String, Integer> getTableReport() {
		return tableReport;
	}

	public String getViewTitle() {
		return viewTitle;
	}

	public TreeMap<String, Integer> getViewReport() {
		return viewReport;
	}

	private String dbTitle;

	private String tableTitle;
	private TreeMap<String, Integer> tableReport;

	private String viewTitle;
	private TreeMap<String, Integer> viewReport;

	/**
	 * Initialize a DBReport
	 * @param reportFilename dbReport filename
	 * @param tableTitle table block begin identifying
	 * @param tableEnd table block end identifying
	 * @param viewTitle view block begin identifying
	 * @param viewEnd view block end identifying
	 * @param dbReportContentLines Array of a report file content lines
	 */
	public DBReport(String reportFilename, String tableTitle, String tableEnd, String viewTitle, String viewEnd, ArrayList<String> dbReportContentLines) {
		if (dbReportContentLines.size() == 0) {
			return;
		}
		this.tableReport = new TreeMap<>();
		iniTreeMapReport(this.tableReport);
		this.viewReport = new TreeMap<>();
		iniTreeMapReport(this.viewReport);

		int tableBeginLineNumber = 0;
		int tableEndLineNumber = 0;
		int viewBeginLineNumber = 0;
		int viewEndLineNumber = 0;

		for (int i = 0; i < dbReportContentLines.size(); ++i) {
			// 依次读取表信息, 视图信息
			String txtLine = dbReportContentLines.get(i);
			if (txtLine.startsWith(tableTitle.substring(0,8))) {
				// 表开始行
				tableBeginLineNumber = i + 1;
				this.tableTitle = txtLine;
			}
			else if (txtLine.startsWith(tableEnd.substring(0,8))) {
				// 表结束行
				tableEndLineNumber = i - 1;
			}
			else if (txtLine.startsWith(viewTitle.substring(0,7))) {
				// 视图开始行
				viewBeginLineNumber = i + 1;
				this.viewTitle = txtLine;
			}
			else if (txtLine.startsWith(viewEnd.substring(0,8))) {
				// 视图结束行
				viewEndLineNumber = i - 1;
			}
			if(tableBeginLineNumber == (i + 1)){
				// 表区之前都视为[头部信息]
				StringBuffer tsb = new StringBuffer();
				tsb.append(String.format("/* file: %s */\n", reportFilename));
				for (int it = 0; it < tableBeginLineNumber - 1; ++it) {
					tsb.append(dbReportContentLines.get(it) + "\n");
				}
				this.dbTitle = tsb.toString();
			}
			if (tableBeginLineNumber > DBTITLE_LINES && tableEndLineNumber == 0 && i >= tableBeginLineNumber) {
				// 索引在表区
				String[] oneLineTxts = txtLine.split("\\t"); // 用tab分割表名和记录数
				this.tableReport.put(oneLineTxts[0], Integer.parseInt(oneLineTxts[1]));
			}
			else if (viewBeginLineNumber > tableEndLineNumber &&  viewEndLineNumber == 0 && i >= viewBeginLineNumber) {
				// 索引在视图区
				String[] oneLineTxts = txtLine.split("\\t"); // 用tab分割表名和记录数
				this.viewReport.put(oneLineTxts[0], Integer.parseInt(oneLineTxts[1]));
			}
		}

	}

	private void iniTreeMapReport(TreeMap<String, Integer> mapReport) {
		mapReport  = new TreeMap<>((o1, o2) -> {
			if(o1 == null || o2 == null)  return 0;
			return o1.toUpperCase().compareTo(o2.toUpperCase());
		});
	}

}
